home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 015 / epsilon.arc / BETTERC.ARC / COMMENT.E < prev    next >
Encoding:
Text File  |  1986-01-03  |  4.5 KB  |  179 lines

  1. /*
  2.  *    Comment manipulation commands for EPSILON.
  3.  *
  4.  *  Copyright (c) 1985 by David Dyer-Bennet
  5.  *  Permission for non-commercial use is hereby granted; all other
  6.  *  rights are reserved.
  7.  *
  8.  *  Written by David Dyer-Bennet
  9.  *  Terrabit Software
  10.  *  4242 Minnehaha Ave S
  11.  *  Minneapolis, MN 55406
  12.  *  Sysop of Fido 14/341, The Terraboard, (612) 721-8967 3/12/24 24hrs
  13.  *  (612) 721-8800 NOT 24 hrs!  More like noon to midnight
  14.  */
  15.  
  16. /*
  17.  *  Revision history:
  18.  *
  19.  *      Edit    Date        Who     Description
  20.  *
  21.  *  Version 1.0
  22.  *      1       3-Jan-86    DD-B    Initial creation
  23.  */
  24.  
  25. /*
  26.  *    The general structure:
  27.  *
  28.  *    Two basic kinds of comments are recognized: end of line comments,
  29.  *    and delimited comments.  The former are composed of an initiating
  30.  *    character sequence, the comment, and an end-of line.  The latter
  31.  *    are composed of an initiating character sequence, the comment (with
  32.  *    as many embedded end-of-lines as desired), and a terminating
  33.  *    character sequence.
  34.  *    
  35.  *    Currently no provision is made for using auto-fill mode when writing
  36.  *    comments.  This should be given consideration as an area for future
  37.  *    development.
  38.  *
  39.  */
  40.  
  41. #include <eel.h>
  42.  
  43. #include "comment.h"
  44.  
  45. #if 0
  46. when_loading ()        /* For testing purposes only */
  47. {
  48.     use_default = 1;
  49.     comment_recog = strsave ("^(^ \t\n)+((/%* )|(/%*))!(.*)%*/$");
  50.     comment_begin = strsave ("/* ");
  51.     comment_end = strsave (" */");
  52.     comment_limit = 65;
  53.     comment_col = 32;
  54.     use_default = 0;
  55. }
  56. #endif
  57.  
  58. command define_comments ()
  59. /*
  60.  *    Prompt the user for the three fields defining comments.  The
  61.  *    RE matching a comment should use ! to leave cursor on the first
  62.  *    character of the text of the comment.
  63.  */
  64. {
  65.     char ls [80];
  66.     
  67.     get_string (ls, "RE matching comments: ");
  68.     if (comment_recog) free (comment_recog);
  69.     comment_recog = strsave (ls);
  70.     get_string (ls, "Comment begin sequence: ");
  71.     if (comment_begin) free (comment_begin);
  72.     comment_begin = strsave (ls);
  73.     get_string (ls, "Comment end sequence (optional): ");
  74.     if (comment_end) free (comment_end);
  75.     if (strlen (ls))
  76.         comment_end = strsave (ls);
  77.     else
  78.         comment_end = NULL;
  79. }
  80.  
  81. int find_comment ()
  82. /*
  83.  *    Using the value in comment_recog, find a comment on the current
  84.  *    line and leave point on the first character of its text.  Return
  85.  *    the character position of the first character of the comment (which 
  86.  *    will probably be to the left of point).  Don't move point if no
  87.  *    comment is to be found.
  88.  */
  89. {
  90.     int origpoint, beglin, endlin, comment_offset;
  91.     int origpos = point;
  92.     int i;
  93.     char *origbuf = bufname;
  94.     char *cr = comment_recog;
  95.     
  96.     to_end_line(); endlin = point;
  97.     to_begin_line(); beglin = point;
  98.     zap ("work_comment");
  99.     xfer ("work_comment", beglin, endlin);
  100.     bufname = "work_comment";
  101.     point = 0;
  102.     i = re_search (1, cr);
  103.     comment_offset = point;
  104.     bufname = origbuf;
  105.     if (i) {
  106.         point += comment_offset;
  107.         return beglin + matchstart;
  108.     } else
  109.         point = origpos;
  110.         return -1;
  111. }
  112.  
  113. command find_make_comment () on reg_tab [ALT(';')]
  114. /*
  115.  *    Find a comment on the current line.  If there isn't one, make one
  116.  *    starting at comment column, or the next tab-stop thereafter; but
  117.  *    don't start one to the right of comment_limit (create blank next line
  118.  *    and put comment there instead).
  119.  */
  120. {
  121.     int i;
  122.     
  123.     i = find_comment ();
  124.     if (i != -1) {
  125.         /*    Comment already present, adjust position */
  126.         point = i;                /* Go to start of comment */
  127.         delete_horizontal_space ();
  128.         i = current_column ();
  129.         to_column (i > comment_col ?
  130.           i + tab_size - i % tab_size : comment_col);
  131.         find_comment ();                /* Get to start of text */
  132.     } else {
  133.         /*    No comment present, make one */
  134.         to_end_line();
  135.         if (current_column () > comment_limit) {
  136.             insert ('\n');
  137.         }
  138.         i = current_column ();
  139.         to_column (i > comment_col ?
  140.           i + tab_size - i % tab_size : comment_col);
  141.         stuff (comment_begin);
  142.         i = point;
  143.         if (comment_end) stuff (comment_end);
  144.         point = i;
  145.     }
  146. }
  147.  
  148. command down_comment_line () on reg_tab [ALT('\n')], reg_tab [CTRL(ALT('m'))]
  149. /*
  150.  *    Go to the next line, and find or make a comment there.
  151.  */
  152. {
  153.     nl_forward ();
  154.     find_make_comment ();
  155. }
  156.  
  157. command delete_comment () on cx_tab [';']
  158. /*
  159.  *    Delete any comment that may be on the current line.  If no comment,
  160.  *    don't move cursor.
  161.  */
  162. {
  163.     int i;
  164.  
  165.     i = find_comment ();
  166.     if (i != -1) {
  167.         point = i;
  168.         delete_horizontal_space ();
  169.         kill_line ();
  170.     }
  171. }
  172.  
  173. command dump_comment ()
  174. {
  175.     say ("recog: %s begin: %s", comment_recog, comment_begin);
  176. }
  177.  
  178. /* End of comment.e */
  179.